package com.cskaoyan.service.mall;

import com.cskaoyan.bean.CommonData;
import com.cskaoyan.bean.po.ohter.MarketBrand;
import com.cskaoyan.bean.po.ohter.MarketBrandExample;
import com.cskaoyan.mapper.MarketBrandMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Author Ajun de BBC
 * @Date 2022/7/20 10:49
 * @Version 1.5
 */

@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    MarketBrandMapper marketBrandMapper;

    /**
     *@Description : 品牌制造商的页面显示与模糊查询
     *@Param :
     *@Return :
     */
    @Override
    public CommonData<MarketBrand> brandList(Integer page, Integer limit, Integer id, String name, String sort, String order) {
        PageHelper.startPage(page,limit);
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        marketBrandExample.setOrderByClause(sort + " " + order);
        MarketBrandExample.Criteria criteria = marketBrandExample.createCriteria();
        name = (name == null) ? "%" : "%" + name + "%";
        criteria.andNameLike(name);
        criteria.andDeletedEqualTo(false);
        if(id != null){
            criteria.andIdEqualTo(id);
        }
        List<MarketBrand> marketBrands = marketBrandMapper.selectByExample(marketBrandExample);

        PageInfo<MarketBrand> marketBrandPageInfo = new PageInfo<>(marketBrands);
        Long total = marketBrandPageInfo.getTotal();
        int pages = marketBrandPageInfo.getPages();

        return new CommonData<MarketBrand>(total.intValue(),pages,limit,page,marketBrands);
    }

    /**
     *@Description : 增加品牌制造商
     *@Param :
     *@Return :
     */
    @Override
    public MarketBrand brandCreate(MarketBrand marketBrand) {
        List<MarketBrand> marketBrands = marketBrandMapper.selectByExample(null);
        for (MarketBrand brand : marketBrands) {
            if(marketBrand.getName().equals(brand.getName())){
                return null;
            }
        }
        marketBrand.setAddTime(new Date(System.currentTimeMillis()));
        marketBrand.setUpdateTime(new Date(System.currentTimeMillis()));
        marketBrandMapper.insertOneBrand(marketBrand);
        return marketBrand;
    }

    /**
     *@Description : 修改品牌制造商
     *@Param :
     *@Return :
     */
    @Override
    public MarketBrand brandUpdate(MarketBrand marketBrand) {
        List<MarketBrand> marketBrands = marketBrandMapper.selectByExample(null);
        for (MarketBrand brand : marketBrands) {
            if(marketBrand.getName().equals(brand.getName())){
                return null;
            }
        }
        marketBrandMapper.updateByPrimaryKeySelective(marketBrand);
        return marketBrand;
    }

    /**
     *@Description : 品牌制造商的逻辑删除
     *@Param :
     *@Return :
     */
    @Override
    public void brandDelete(MarketBrand marketBrand) {
        marketBrand.setUpdateTime(new Date(System.currentTimeMillis()));
        marketBrand.setDeleted(true);
        marketBrandMapper.updateByPrimaryKey(marketBrand);
    }


}
